package listbyorder.access001_100.test36;

import java.util.HashSet;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/5/31 8:23
 */
public class Solution1 {

    // 方法一：暴力破解
    // 首先判断行是否满足条件
    // 判断列是否满足条件
    // 判断小九格是否满足
    public boolean isValidSudoku(char[][] board) {        // 首先判断行
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < 9; i++) {
            set.clear();
            for (int j = 0; j < 9; j++) {
                if(board[i][j] != '.'){
                    if(set.contains(board[i][j])){
                        return false;
                    }
                    set.add(board[i][j]);
                }
            }
        }

        // 然后判断列
        for (int j = 0; j < 9; j++) {
            set.clear();
            for (int i = 0; i < 9; i++) {
                if(board[i][j] != '.'){
                    if(set.contains(board[i][j])){
                        return false;
                    }
                    set.add(board[i][j]);
                }
            }
        }

        // 然后9 * 9 的网格
        for (int i = 0; i < 9; i++) {
            int s_r = (i / 3) * 3;
            int s_c = (i % 3) * 3;
            set.clear();
            for (int j = s_r; j < s_r + 3; j++) {
                for (int k = s_c; k < s_c + 3; k++) {
                    if(board[j][k] != '.'){
                        if(set.contains(board[j][k])){
                            return false;
                        }
                        set.add(board[j][k]);
                    }
                }
            }
        }
        return true;

    }
}
